package com.sqx.modules.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqx.common.utils.Result;
import com.sqx.modules.task.dao.TbDailyTasksDao;
import com.sqx.modules.task.entity.TbDailyTasks;
import com.sqx.modules.task.service.TbDailyTasksService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Admin
* @description 针对表【tb_daily_tasks(每日任务表)】的数据库操作Service实现
* @createDate 2024-07-22 10:56:35
*/
@Service
public class TbDailyTasksServiceImpl extends ServiceImpl<TbDailyTasksDao, TbDailyTasks>
    implements TbDailyTasksService {


    /**
     * 添加实名认证的任务
     */
    public static final String REAL_NAME_AUTH = "REAL_NAME_AUTH";
    /**
     * 添加邀请好友签到的任务
     */
    public static final String INVITATION_FRIENDS = "INVITATION_FRIENDS";
    /**
     * 添加商品发布的任务
     */
    public static final String PRODUCT_POSTING = "PRODUCT_POSTING";
    /**
     * 添加商品买卖的任务
     */
    public static final String PRODUCT_TRANSACTION = "PRODUCT_TRANSACTION";
    @Override
    public Result getTbDailyTasksList(Integer pageNum, Integer pageSize, TbDailyTasks tbDailyTasks) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbDailyTasks> tasksList = baseMapper.selectList(new QueryWrapper<>());
        return Result.success().put("data",new PageInfo<>(tasksList));
    }

    @Override
    public Result saveOrUpdateTbDailyTasks(TbDailyTasks tbDailyTasks) {
        if (tbDailyTasks.getId() != null){
            TbDailyTasks tasks = baseMapper.selectById(tbDailyTasks.getId());
            if (tasks == null){
                return Result.error("该任务不存在");
            }
            if (tbDailyTasks.getType() == 1){
                tbDailyTasks.setTaskType(REAL_NAME_AUTH);
            }else if (tbDailyTasks.getType() == 2){
                tbDailyTasks.setTaskType(INVITATION_FRIENDS);
            }else if (tbDailyTasks.getType() == 3){
                tbDailyTasks.setTaskType(PRODUCT_POSTING);
            }else if (tbDailyTasks.getType() == 4){
                tbDailyTasks.setTaskType(PRODUCT_TRANSACTION);
            }
            return Result.upStatus(baseMapper.updateById(tbDailyTasks));
        }else {
            if (tbDailyTasks.getType() == 1){
                tbDailyTasks.setTaskType(REAL_NAME_AUTH);
            }else if (tbDailyTasks.getType() == 2){
                tbDailyTasks.setTaskType(INVITATION_FRIENDS);
            }else if (tbDailyTasks.getType() == 3){
                tbDailyTasks.setTaskType(PRODUCT_POSTING);
            }else if (tbDailyTasks.getType() == 4){
                tbDailyTasks.setTaskType(PRODUCT_TRANSACTION);
            }
            return Result.upStatus(baseMapper.insert(tbDailyTasks));
        }
    }

    @Override
    public Result deleteTbDailyTasks(Long id) {
        TbDailyTasks tasks = baseMapper.selectById(id);
        if (tasks != null){
            return Result.upStatus(baseMapper.deleteById(id));
        }
        return Result.error("该任务不存在");
    }

    @Override
    public Result getTbDailyTasksById(Long id) {
        TbDailyTasks tasks = baseMapper.selectById(id);
        if (tasks != null){
            return Result.success().put("data",tasks);
        }
        return Result.error("该任务不存在");
    }
}




